package sz.szsmk.citizencard.util;

import android.content.Context;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.TreeMap;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.model.network.bean.request.IBaseRequest;

public class IObjectSignatureUtil {

    public static IBaseRequest getBaseRequest() {
        Context context = DaggerApplication.getIns();
        //UserBean userBean = DaggerApplication.getIns().getMainAppUserBean();
        /*if (userBean == null) {
            return null;
        }*/
//        PreAuthResult preAuthResult = GlobalVariables.getInstance().getPreAuthResult();
        IBaseRequest baseRequest = new IBaseRequest();
//        baseRequest.setAccess_token(userBean.getData().getAccess_token());
        baseRequest.setAccess_token("5463d411b93f80c5f9e169d718b65025");
        baseRequest.setDevice_id(AndroidUtils.getAndroidId(context));
        baseRequest.setSign("8AD4BE2948A48DE06E487FFB0619449E");
//        CommonRequest commonRequest = new CommonRequest();

//        commonRequest.setOpenId(preAuthResult.getOpenId());
//        signatureRequest(baseRequest, userBean.getData().getRefresh_token());
        return baseRequest;
    }

    /*public static void signaturePreAuthRequest(IBaseRequest obj) {
        String key = RequestParamUtils.getKey();
        signatureRequest(obj, key);
    }*/

    /*public static void signatureRequest(IBaseRequest obj) {
        Context context = DZSmkAppInit.getInstance().getContext();
        PreAuthResult preAuthResult = GlobalVariables.getInstance().getPreAuthResult();
        if (preAuthResult == null) {
            return;
        }
        obj.setOpenId(preAuthResult.getOpenId());
        Map<String, String> stringMap = toMap(obj);
        String signature = SIgnUtil.generateSign(stringMap, preAuthResult.getToken());
        obj.setSignature(signature);
    }*/



    public static Map<String, String> toMap(Object obj) {
        Class<?> aClass = obj.getClass();
        Map<String, String> maps = new TreeMap<>();
        while (null != aClass && !aClass.getName().equals("java.lang.Object")) {
            Method[] declaredMethods = aClass.getDeclaredMethods();
            for (Method declaredMethod : declaredMethods) {
                if (declaredMethod.getName().startsWith("get")) {
                    String methodName = declaredMethod.getName();
                    String upField = methodName.substring(3, methodName.length());
                    String sField = upField.substring(0, 1).toLowerCase() + upField.substring(1, upField.length());
                    try {
                        Object retVal = declaredMethod.invoke(obj);
                        if (null != retVal) {
                            maps.put(sField, retVal.toString());
                        }
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    } catch (InvocationTargetException e) {
                        e.printStackTrace();
                    }

                }
            }
            aClass = aClass.getSuperclass();
        }
        return maps;

    }

}